package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.csaba.connector.model.Account;
import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.DynamicColumnLayout;
import com.csaba.swing.gui.util.TableSizeContainer;
import com.csaba.swing.gui.util.WindowSizeContanier;

@SuppressWarnings( "serial" )
public class AccountView extends JInternalFrame implements ContextListener
{
	JLabel bank;
	JLabel customer;
	JLabel number;
	JLabel name;
	JLabel type;
	JLabel available;
	JLabel booked;
	JTable nativeFields;
	Account account;

	public AccountView()
	{
		super(MainWindow.getString("AccountView.title"), true, true, true, true); //$NON-NLS-1$
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		if ( !WindowSizeContanier.getInstance().monitorComponent(this) )
			setSize(400, 350);
		setFrameIcon(GUIContext.getIcon("accounts", 16)); //$NON-NLS-1$

		//init layout
		final JPanel general = new JPanel(new DynamicColumnLayout(new int[] { DynamicColumnLayout.RIGHT,
				DynamicColumnLayout.LEFT }));
		general.add(new JLabel(MainWindow.getString("AccountView.bank"))); //$NON-NLS-1$
		general.add(bank = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.customer"))); //$NON-NLS-1$
		general.add(customer = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.number"))); //$NON-NLS-1$
		general.add(number = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.name"))); //$NON-NLS-1$
		general.add(name = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.type"))); //$NON-NLS-1$
		general.add(type = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.availBalance"))); //$NON-NLS-1$
		general.add(available = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("AccountView.bookedBalance"))); //$NON-NLS-1$
		general.add(booked = new JLabel("")); //$NON-NLS-1$

		final JPanel buttons = new JPanel();
		buttons.add(new JButton(new AbstractAction(MainWindow.getString("AccountView.button.close")) //$NON-NLS-1$
				{

					@Override
					public void actionPerformed( final ActionEvent arg0 )
					{
						dispose();
					}

				}));

		nativeFields = new JTable(new Object[0][2], new String[] {
				MainWindow.getString("AccountView.header.name"), MainWindow.getString("AccountView.header.value") }); //$NON-NLS-1$ //$NON-NLS-2$
		final JScrollPane scrollPane = new JScrollPane(nativeFields);
		nativeFields.setFillsViewportHeight(true);

		final JPanel contentPane = (JPanel) getContentPane();
		contentPane.setBorder(BorderFactory.createEmptyBorder());
		contentPane.setLayout(new BorderLayout());
		contentPane.add(general, BorderLayout.NORTH);
		contentPane.add(buttons, BorderLayout.SOUTH);
		contentPane.add(scrollPane, BorderLayout.CENTER);
		GUIContext.getInstance().addContextListener(this);
	}

	public void showIt( final Account account )
	{
		this.account = account;
		final Customer customer = account.getCustomer();
		bank.setText(customer.getBank().getName());
		this.customer.setText(customer.getName() + " (" + customer.getId() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		number.setText(account.getNumber());
		name.setText(account.getName());
		type.setText(account.getType());
		available.setText(account.getAvailableBalance() != null ? account.getAvailableBalance().toString() : "");
		booked.setText(account.getBookedBalance() != null ? account.getBookedBalance().toString() : "");

		final String[] propNames = account.getRemotePropertyNames();
		Arrays.sort(propNames);
		final String[][] tableModel = new String[propNames.length][2];
		for ( int i = 0; i < tableModel.length; i++ )
		{
			tableModel[i][0] = account.getLocalizedName(propNames[i]);
			tableModel[i][1] = account.getRemoteProperty(propNames[i]).toString();
		}
		nativeFields.setModel(new DefaultTableModel(tableModel, new String[] {
				MainWindow.getString("AccountView.header.name"), MainWindow.getString("AccountView.header.value") }) //$NON-NLS-1$ //$NON-NLS-2$
				{
					@Override
					public boolean isCellEditable( final int row, final int column )
					{
						return false;
					}
				});

		setVisible(true);
		TableSizeContainer.getInstance().monitorTable("AccountView.nativeFields", nativeFields); //$NON-NLS-1$
	}

	@Override
	public void contextEventPerformed( final int eventType )
	{
		if ( eventType == SESSION_REMOVED )
		{
			final Customer customer = account.getCustomer();
			final List<Session> sessions = GUIContext.getInstance().getSessions();
			boolean sessionStillActive = false;
			for ( final Session session : sessions )
			{
				if ( session.getCustomer() == customer )
				{
					sessionStillActive = true;
					break;
				}
			}
			if ( !sessionStillActive )
			{
				dispose();
			}
		}

	}

}
